package com.huadao.cart_ccs.experiment.changeLiquidDialog;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.base.BaseDialogFragment;
import com.huadao.cart_ccs.base.BasePresenter;
import com.huadao.cart_ccs.data.bo.ChangeLiquidAmount;
import com.huadao.cart_ccs.data.task.ChangeLiquidBO;
import com.huadao.cart_ccs.data.task.LiquidLoopTask;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

//这是徐鹏的版本
public class ChangeLiquidDF extends BaseDialogFragment {

    @BindView(R.id.et_firstTurn_firstTime_amount)
    EditText etFirstTurnFirstTimeAmount;
    @BindView(R.id.et_firstTurn_secondTime_amount)
    EditText etFirstTurnSecondTimeAmount;
    @BindView(R.id.et_firstTurn_thirdTime_amount)
    EditText etFirstTurnThirdTimeAmount;
    @BindView(R.id.et_secondTurn_firstTime_amount)
    EditText etSecondTurnFirstTimeAmount;
    @BindView(R.id.et_secondTurn_secondTime_amount)
    EditText etSecondTurnSecondTimeAmount;
    @BindView(R.id.et_secondTurn_thirdTime_amount)
    EditText etSecondTurnThirdTimeAmount;
    @BindView(R.id.et_thirdTurn_firstTime_amount)
    EditText etThirdTurnFirstTimeAmount;
    @BindView(R.id.et_thirdTurn_secondTime_amount)
    EditText etThirdTurnSecondTimeAmount;
    @BindView(R.id.et_thirdTurn_thirdTime_amount)
    EditText etThirdTurnThirdTimeAmount;
    @BindView(R.id.btn_cancel)
    Button btnCancel;
    @BindView(R.id.btn_saveOrEdit)
    Button btnSaveOrEdit;
    @BindView(R.id.et_firstSecond_time)
    EditText etFirstSecondTime;
    @BindView(R.id.et_secondThird_time)
    EditText etSecondThirdTime;

    //修改置换
    private ChangeLiquidBO changeLiquidBO;

    private OnChangeLiquid onChangeLiquid;

    public interface OnChangeLiquid {
        void onSaveChangeLiquid(ChangeLiquidBO changeLiquidBO);

        void onEditChangeLiquid(ChangeLiquidBO changeLiquidBO);
    }

    public static ChangeLiquidDF newInstance(ChangeLiquidBO changeLiquidBO) {
        ChangeLiquidDF changeLiquidDF = new ChangeLiquidDF();
        if (changeLiquidBO != null) {
            Bundle bundle = new Bundle();
            bundle.putSerializable("ChangeLiquid", changeLiquidBO);
            changeLiquidDF.setArguments(bundle);
        }
        return changeLiquidDF;
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialogfrag_changeliquid;
    }

    @Override
    protected void onInit() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            changeLiquidBO = (ChangeLiquidBO) bundle.get("ChangeLiquid");
        }
//        if (changeLiquidBO != null) {
//            btnSaveOrEdit.setText(R.string.edit);
//        } else {
//            btnSaveOrEdit.setText(R.string.save);
//        }
        btnSaveOrEdit.setText(R.string.save);
        if (changeLiquidBO != null) {
            initData();
        }
        getDialog().setCanceledOnTouchOutside(false);
    }

    private void initData() {
        List<ChangeLiquidBO.ChangeLiquidTurn> changeLiquidTurnList = changeLiquidBO.getChangeLiquidTurnList();
        if (changeLiquidTurnList == null || changeLiquidTurnList.size() == 0) {
            return;
        }
        if (changeLiquidTurnList.size() > 0) {
            ChangeLiquidBO.ChangeLiquidTurn changeLiquidTurn = changeLiquidTurnList.get(0);
            List<ChangeLiquidAmount> firstTurnList = changeLiquidTurn.getAmountList();
            if (firstTurnList.size() > 0) {
                etFirstTurnFirstTimeAmount.setText(String.valueOf(firstTurnList.get(0)));
            } else {
                etFirstTurnFirstTimeAmount.setText("0");
            }
            if (firstTurnList.size() > 1) {
                etFirstTurnSecondTimeAmount.setText(String.valueOf(firstTurnList.get(1)));
            } else {
                etFirstTurnSecondTimeAmount.setText("0");
            }
            if (firstTurnList.size() > 2) {
                etFirstTurnThirdTimeAmount.setText(String.valueOf(firstTurnList.get(2)));
            } else {
                etFirstTurnThirdTimeAmount.setText("0");
            }
            etFirstSecondTime.setText(String.valueOf(changeLiquidTurn.getIntervalTime()/60));
        } else {
            etFirstTurnFirstTimeAmount.setText("0");
            etFirstTurnSecondTimeAmount.setText("0");
            etFirstTurnThirdTimeAmount.setText("0");
            etFirstSecondTime.setText("0");
        }
        if (changeLiquidTurnList.size() > 1) {
            ChangeLiquidBO.ChangeLiquidTurn changeLiquidTurn = changeLiquidTurnList.get(1);
            List<ChangeLiquidAmount> secondTurnList = changeLiquidTurn.getAmountList();
            if (secondTurnList.size() > 0) {
                etSecondTurnFirstTimeAmount.setText(String.valueOf(secondTurnList.get(0)));
            } else {
                etSecondTurnFirstTimeAmount.setText("0");
            }
            if (secondTurnList.size() > 1) {
                etSecondTurnSecondTimeAmount.setText(String.valueOf(secondTurnList.get(1)));
            } else {
                etSecondTurnSecondTimeAmount.setText("0");
            }
            if (secondTurnList.size() > 2) {
                etSecondTurnThirdTimeAmount.setText(String.valueOf(secondTurnList.get(2)));
            } else {
                etSecondTurnThirdTimeAmount.setText("0");
            }
            etSecondThirdTime.setText(String.valueOf(changeLiquidTurn.getIntervalTime()/60));
        } else {
            etSecondTurnFirstTimeAmount.setText("0");
            etSecondTurnSecondTimeAmount.setText("0");
            etSecondTurnThirdTimeAmount.setText("0");
            etSecondThirdTime.setText("0");
        }
        if (changeLiquidTurnList.size() > 2) {
            ChangeLiquidBO.ChangeLiquidTurn changeLiquidTurn = changeLiquidTurnList.get(2);
            List<ChangeLiquidAmount> thirdTurnList = changeLiquidTurn.getAmountList();
            if (thirdTurnList.size() > 0) {
                etThirdTurnFirstTimeAmount.setText(String.valueOf(thirdTurnList.get(0)));
            } else {
                etThirdTurnFirstTimeAmount.setText("0");
            }
            if (thirdTurnList.size() > 1) {
                etThirdTurnSecondTimeAmount.setText(String.valueOf(thirdTurnList.get(1)));
            } else {
                etThirdTurnSecondTimeAmount.setText("0");
            }
            if (thirdTurnList.size() > 2) {
                etThirdTurnThirdTimeAmount.setText(String.valueOf(thirdTurnList.get(2)));
            } else {
                etThirdTurnThirdTimeAmount.setText("0");
            }
        } else {
            etThirdTurnFirstTimeAmount.setText("0");
            etThirdTurnSecondTimeAmount.setText("0");
            etThirdTurnThirdTimeAmount.setText("0");
        }

    }

    @Override
    protected boolean isDialog() {
        return true;
    }

    @OnClick({R.id.btn_cancel, R.id.btn_saveOrEdit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_cancel:
                this.dismiss();
                break;
            case R.id.btn_saveOrEdit:
                String err = checkParams();
                if (err != null) {
                    showDialogMessage(null, err);
                    return;
                }
                if (btnSaveOrEdit.getText().toString().equals(getString(R.string.save))) {
                    save();
                } else if (btnSaveOrEdit.getText().toString().equals(getString(R.string.edit))) {
                    edit();
                }
                this.dismiss();
                break;
        }
    }

    public void setOnChangeLiquid(OnChangeLiquid onChangeLiquid) {
        this.onChangeLiquid = onChangeLiquid;
    }

    private void edit() {
        changeLiquidBO = generateBO();
        if (onChangeLiquid != null) {
            onChangeLiquid.onEditChangeLiquid(changeLiquidBO);
        }
    }

    private void save() {
        ChangeLiquidBO changeLiquidBO = generateBO();
        if (onChangeLiquid != null) {
            onChangeLiquid.onSaveChangeLiquid(changeLiquidBO);
        }
    }

    private String checkParams() {
        String err=null;
        if (TextUtils.isEmpty(etFirstTurnFirstTimeAmount.getText())) {
            err = "请输入第一轮第一次置换量";
        } else if (TextUtils.isEmpty(etFirstTurnSecondTimeAmount.getText())) {
            err = "请输入第一轮第二次置换量";
        } else if (TextUtils.isEmpty(etFirstTurnThirdTimeAmount.getText())) {
            err = "请输入第一轮第三次置换量";
        } else if (TextUtils.isEmpty(etFirstSecondTime.getText())) {
            err = "请输入第一轮于第二轮的间隔时间";
        } else if (TextUtils.isEmpty(etSecondTurnFirstTimeAmount.getText())) {
            err = "请输入第二轮第一次置换量";
        } else if (TextUtils.isEmpty(etSecondTurnSecondTimeAmount.getText())) {
            err = "请输入第二轮第二次置换量";
        } else if (TextUtils.isEmpty(etSecondTurnThirdTimeAmount.getText())) {
            err = "请输入第二轮第三次置换量";
        } else if (TextUtils.isEmpty(etSecondThirdTime.getText())) {
            err = "请输入第二轮于第三轮的间隔时间";
        } else if (TextUtils.isEmpty(etThirdTurnFirstTimeAmount.getText())) {
            err = "请输入第三轮第一次置换量";
        } else if (TextUtils.isEmpty(etThirdTurnSecondTimeAmount.getText())) {
            err = "请输入第三轮第二次置换量";
        } else if (TextUtils.isEmpty(etThirdTurnThirdTimeAmount.getText())) {
            err = "请输入第三轮第三次置换量";
        }
        if(err!=null){
            return err;
        }
        if(etFirstTurnFirstTimeAmount.getText().toString().equals("0")){
            err = "第一轮第一次置换量不能为0";
        }else if(!etFirstTurnFirstTimeAmount.getText().toString().equals("0")){
            if(etFirstTurnSecondTimeAmount.getText().toString().equals("0")&&!etFirstTurnThirdTimeAmount.getText().toString().equals("0")){
                err = "第一轮第三次置换量只能为0";
            }
        }
        if(etSecondTurnFirstTimeAmount.getText().toString().equals("0")){
            if(!etSecondTurnSecondTimeAmount.getText().toString().equals("0")){
                err = "第二轮第二次置换量只能为0";
            }else if(!etSecondTurnThirdTimeAmount.getText().toString().equals("0")){
                err = "第二轮第三次置换量只能为0";
            }
        }else if(!etSecondTurnFirstTimeAmount.getText().toString().equals("0")){
            if(etSecondTurnSecondTimeAmount.getText().toString().equals("0")&&!etSecondTurnThirdTimeAmount.getText().toString().equals("0")){
                err = "第二轮第三次置换量只能为0";
            }
        }
        if(etThirdTurnFirstTimeAmount.getText().toString().equals("0")){
            if(!etThirdTurnSecondTimeAmount.getText().toString().equals("0")){
                err = "第三轮第二次置换量只能为0";
            }else if(!etThirdTurnThirdTimeAmount.getText().toString().equals("0")){
                err = "第三轮第三次置换量只能为0";
            }
        }else if(!etThirdTurnFirstTimeAmount.getText().toString().equals("0")){
            if(etThirdTurnSecondTimeAmount.getText().toString().equals("0")&&!etThirdTurnThirdTimeAmount.getText().toString().equals("0")){
                err = "第三轮第三次置换量只能为0";
            }
        }else {
            err = null;
        }
        return err;
    }

    private ChangeLiquidBO generateBO() {
        ChangeLiquidBO changeLiquidBO = new ChangeLiquidBO();
        int firstTurnFirstTime = Integer.parseInt(etFirstTurnFirstTimeAmount.getText().toString());
        int firstTurnSecondTime = Integer.parseInt(etFirstTurnSecondTimeAmount.getText().toString());
        int firstTurnThirdTime = Integer.parseInt(etFirstTurnThirdTimeAmount.getText().toString());
        int firstSecondTime = Integer.parseInt(etFirstSecondTime.getText().toString());
        ChangeLiquidBO.ChangeLiquidTurn firstTurn = changeLiquidBO.new ChangeLiquidTurn();
        firstTurn.add(new ChangeLiquidAmount(firstTurnFirstTime,firstTurnFirstTime,LiquidLoopTask.Liquid_saline));
        firstTurn.add(new ChangeLiquidAmount(firstTurnSecondTime,firstTurnSecondTime,LiquidLoopTask.Liquid_saline));
        firstTurn.add(new ChangeLiquidAmount(firstTurnThirdTime,firstTurnThirdTime,LiquidLoopTask.Liquid_saline));
        firstTurn.setIntervalTime(firstSecondTime*60);
        int secondTurnFirstTime = Integer.parseInt(etSecondTurnFirstTimeAmount.getText().toString());
        int secondTurnSecondTime = Integer.parseInt(etSecondTurnSecondTimeAmount.getText().toString());
        int secondTurnThirdTime = Integer.parseInt(etSecondTurnThirdTimeAmount.getText().toString());
        int secondThirdTime = Integer.parseInt(etSecondThirdTime.getText().toString());
        ChangeLiquidBO.ChangeLiquidTurn secondTurn = changeLiquidBO.new ChangeLiquidTurn();
        secondTurn.add(new ChangeLiquidAmount(secondTurnFirstTime,secondTurnFirstTime,LiquidLoopTask.Liquid_saline));
        secondTurn.add(new ChangeLiquidAmount(secondTurnSecondTime,secondTurnSecondTime,LiquidLoopTask.Liquid_saline));
        secondTurn.add(new ChangeLiquidAmount(secondTurnThirdTime,secondTurnThirdTime,LiquidLoopTask.Liquid_saline));
        secondTurn.setIntervalTime(secondThirdTime*60);
        int thirdTurnFirstTime = Integer.parseInt(etThirdTurnFirstTimeAmount.getText().toString());
        int thirdTurnSecondTime = Integer.parseInt(etThirdTurnSecondTimeAmount.getText().toString());
        int thirdTurnThirdTime = Integer.parseInt(etThirdTurnThirdTimeAmount.getText().toString());
        ChangeLiquidBO.ChangeLiquidTurn thirdTurn = changeLiquidBO.new ChangeLiquidTurn();
        thirdTurn.add(new ChangeLiquidAmount(thirdTurnFirstTime,thirdTurnFirstTime,LiquidLoopTask.Liquid_saline));
        thirdTurn.add(new ChangeLiquidAmount(thirdTurnSecondTime,thirdTurnSecondTime,LiquidLoopTask.Liquid_saline));
        thirdTurn.add(new ChangeLiquidAmount(thirdTurnThirdTime,thirdTurnThirdTime,LiquidLoopTask.Liquid_saline));
        thirdTurn.setIntervalTime(0);

        changeLiquidBO.addTurn(firstTurn);
        changeLiquidBO.addTurn(secondTurn);
        changeLiquidBO.addTurn(thirdTurn);

        return changeLiquidBO;
    }
}
